package com.asion.qkan.ui.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.asion.qkan.R;
import com.asion.qkan.adapter.CollectioAdapter;
import com.asion.qkan.db.Cartoon;
import com.asion.qkan.jiguangtuisong.ExampleApplication;
import com.asion.qkan.ui.view.AllActivity;

import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class CollectionFragment extends BaseFragment {


    @BindView(R.id.subcategory_item_recycler)
    RecyclerView recyclerView;
    @BindView(R.id.subcategory_item_swipe)
    SwipeRefreshLayout swipeRefreshLayout;
    private CollectioAdapter adapter;

    public CollectionFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.subcategory_item;
    }

    @Override
    public void initPresenter() {
    }

    @Override
    protected void initView() {

        adapter = new CollectioAdapter(getContext());
        recyclerView.setAdapter(adapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        adapter.setItemClickListener(cartoon -> {
            Bundle bundble = new Bundle();
            bundble.putString("id", String.valueOf(cartoon.getId()));
            bundble.putString("title",cartoon.getTitle());
            bundble.putString("vertical_image_url",cartoon.getImage());
            AllActivity.startAction(getActivity(),bundble);
        });
        List<Cartoon> list= ExampleApplication.getCartoonDao().queryBuilder().list();
        adapter.addAll(list);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        adapter.removeAll(list);
                        List<Cartoon> list1= ExampleApplication.getCartoonDao().queryBuilder().list();
                        adapter.addAll(list1);
                        mhandler.sendEmptyMessage(1);
                    }
                }, 3000);
            }
        });
    }
    private Handler mhandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    swipeRefreshLayout.setRefreshing(false);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(getContext(), "刷新成功", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

}
